package top.malaoshi.webmvc;

import top.malaoshi.util.StringUtil;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DefaultServletHttpRequestHandler {
    /**
     * Tomcat, Jetty, JBoss, and GlassFish 自带的默认Servlet的名字
     */
    private static final String COMMON_DEFAULT_SERVLET_NAME = "default";
    /**
     * Google App Engine 自带的 默认Servlet的名字
     */
    private static final String GAE_DEFAULT_SERVLET_NAME = "_ah_default";
    /**
     * Resin 自带的 默认Servlet的名字
     */
    private static final String RESIN_DEFAULT_SERVLET_NAME = "resin-file";
    /**
     * WebLogic 自带的 默认Servlet的名字
     */
    private static final String WEBLOGIC_DEFAULT_SERVLET_NAME = "FileServlet";
    /**
     * WebSphere  自带的 默认Servlet的名字
     */
    private static final String WEBSPHERE_DEFAULT_SERVLET_NAME = "SimpleFileServlet";

    private String defaultServletName;

    private ServletContext servletContext;

    public void setDefaultServletName(String defaultServletName) {
        this.defaultServletName = defaultServletName;
    }

    /**
     * 必须调用此方法，该方法可获取不同Servlet容器的默认Servlet对象
     * @param servletContext
     */
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
        if (StringUtil.isEmpty(this.defaultServletName)) {
            if (this.servletContext.getNamedDispatcher(COMMON_DEFAULT_SERVLET_NAME) != null) {
                this.defaultServletName = COMMON_DEFAULT_SERVLET_NAME;
            }
            else if (this.servletContext.getNamedDispatcher(GAE_DEFAULT_SERVLET_NAME) != null) {
                this.defaultServletName = GAE_DEFAULT_SERVLET_NAME;
            }
            else if (this.servletContext.getNamedDispatcher(RESIN_DEFAULT_SERVLET_NAME) != null) {
                this.defaultServletName = RESIN_DEFAULT_SERVLET_NAME;
            }
            else if (this.servletContext.getNamedDispatcher(WEBLOGIC_DEFAULT_SERVLET_NAME) != null) {
                this.defaultServletName = WEBLOGIC_DEFAULT_SERVLET_NAME;
            }
            else if (this.servletContext.getNamedDispatcher(WEBSPHERE_DEFAULT_SERVLET_NAME) != null) {
                this.defaultServletName = WEBSPHERE_DEFAULT_SERVLET_NAME;
            }
            else {
                throw new IllegalStateException("Unable to locate the default servlet for serving static content. " +
                        "Please set the 'defaultServletName' property explicitly.");
            }
        }
    }

    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher rd = this.servletContext.getNamedDispatcher(this.defaultServletName);
        if (rd == null) {
            throw new IllegalStateException("A RequestDispatcher could not be located for the default servlet '" + this.defaultServletName + "'");
        } else {
            rd.forward(request, response);
        }
    }
}
